<?php
/**
 * 清理一个店铺下的所有店员及关联的数据
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/28 11:23
 * @copyright PanxSoft Inc.
 */

namespace App\Services\DBHelpers;


use App\Models\Admin\StoreAdmin;
use App\Models\Logger\OperateLog;
use App\Models\Panx\SkinRecordBonus;
use App\Models\Store\HmSkinRecord;
use App\Models\Store\SkinRecord;
use App\Models\Store\Store;
use App\Models\Store\WaxSkinRecord;
use App\Services\Utils\Transaction;
use DB;
use Illuminate\Database\Eloquent\Collection;

class StoreAdminCleaner
{
    /**
     * @example
     *      App\Services\DBHelpers\StoreAdminCleaner::clean()
     *
     * @param int $store_id
     *
     * @return string
     * @throws \Exception
     */
    public static function clean(int $store_id)
    {
        return 'Dangers !!!';

        /** @var Store $store */
        $store = Store::find($store_id);

        Transaction::handle(function () use ($store) {

            /** @var Collection $staffs */
            $staffs = $store->storeStaffs;
            $staff_ids = $staffs->pluck('id')->all();

            echo 'Deleting Staff Count: '. count($staffs) . PHP_EOL;

            $staffs->each(function (StoreAdmin $staff) {
                echo 'Deleting: '. $staff->account . PHP_EOL;

                $skin_record_ids = self::cleanSkinRecords($staff);

                self::cleanBonus($skin_record_ids);

                self::cleanLogs($staff);

                $staff->syncRoles([]);
            });

            $staff_cnt = StoreAdmin::where('id', $staff_ids)->delete();

            echo 'Staffs Deleting: '. $staff_cnt . PHP_EOL;
        });

    }


    /**
     * @param StoreAdmin $staff
     *
     * @return array
     * @throws \Exception
     */
    static private function cleanSkinRecords(StoreAdmin $staff): array
    {
        $skin_record_ids = $staff->skinRecords()->pluck('id')->all();

        echo 'Skin Record Deleting: ' . count($skin_record_ids) . PHP_EOL;

        $hm_record_ids = $staff->skinRecords()
            ->where('skin_detector_id', SkinRecord::HM)
            ->pluck('id')
            ->all();

        $hms = HmSkinRecord::whereIn('skin_record_id', $hm_record_ids)->delete();

        echo 'Hm Skin Record Deleting: ' . $hms . PHP_EOL;

        $wax_record_ids = $staff->skinRecords()
            ->where('skin_detector_id', SkinRecord::WAX)
            ->pluck('id')
            ->all();

        $wax = WaxSkinRecord::whereIn('skin_record_id', $wax_record_ids)->delete();

        echo 'Wax Skin Record Deleting: ' . $wax . PHP_EOL;

        $staff->skinRecords()->delete();

        return $skin_record_ids;
    }


    /**
     * @param StoreAdmin $staff
     *
     * @throws \Exception
     */
    static private function cleanLogs(StoreAdmin $staff): void
    {
        $opt_logs = OperateLog::where([
            'operatorable_id'   => $staff->getKey(),
            'operatorable_type' => class_basename($staff),
        ])->delete();

        $logs = OperateLog::where([
            'logable_id'   => $staff->getKey(),
            'logable_type' => class_basename($staff),
        ])->delete();

        echo 'Opt Logs Deleting: ' . $opt_logs . PHP_EOL;
        echo 'Logs Deleting: ' . $logs . PHP_EOL;
    }


    /**
     * @param $skin_record_ids
     *
     * @throws \Exception
     */
    static private function cleanBonus($skin_record_ids): void
    {
        $bonus = SkinRecordBonus::whereIn('skin_record_id', $skin_record_ids)->delete();

        echo 'Skin Bonus Deleting: ' . $bonus . PHP_EOL;
    }
}